{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    管理用户
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">资产管理</a></li>
        <li class="active">管理用户</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    新增管理用户
{% endblock %}

{% block body_form %}
    <form name="assetForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">管理用户</p>

            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>名称</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="name" placeholder="请填写用户名称" data-rule="名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger"></span>管理用户</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="username" placeholder="请填写管理用户" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger"></span>用户密码</label>
                            <div class="col-lg-8">
                                <input type="password" class="form-control" name="password" placeholder="请填写用户密码" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                    <fieldset>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-8">
                                <input id="showPasswd" class="magic-checkbox" type="checkbox">
                                <label for="showPasswd">显示密码</label>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger"></span>私钥文件</label>
                            <div class="col-lg-8">
                                <textarea class="form-control" rows="8" name="private_key"></textarea>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right submit-btn" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">
        $(function () {
            // 显示或者隐藏密码
            $("#showPasswd").on('click', function () {
                var isCheck = $(this).prop('checked');
                if(isCheck){
                    $(this).closest('form').find('input[name="password"]').attr("type", "text");
                }else{
                    $(this).closest('form').find('input[name="password"]').attr("type", "password");
                }
            });
        });

        $(function () {
            var $formEle = $('form[name="assetForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                $.ajax({
                    type: 'post',
                    url: '{{ url_for("assets.adminuser-create") }}',
                    data: {
                        'name': $('input[name="name"]').val(),
                        'username': $('input[name="username"]').val(),
                        'password': $('input[name="password"]').val(),
                        'private_key': $('textarea[name="private_key"]').val()
                    },
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '新增成功'
                            });
                            window.location.href = '{{ url_for("assets.adminuser-list") }}'
                        }
                    }
                });
            });

        })

    </script>

{% endblock %}
